package com.elm.cart.controller;


import com.elm.cart.pojo.Cart;
import com.elm.cart.service.CartService;
import com.elm.common.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/list")
    public Result<List<Cart>> listCart(@RequestParam(required = false) Integer businessId,
                                       @RequestParam(required = true) String  userId){
        List<Cart> list=cartService.listCart(userId,businessId);
        if(list.size()!=0){
            return Result.success(list);
        }
        return Result.success();
    }

    @PostMapping("/save")
    public Result<Integer> saveCart(@RequestBody Cart cart){
        Integer rows=cartService.saveCart(cart);
        if (rows!=0){
            return Result.success(rows);
        }
        return Result.error("插入失败");
    }

    @PutMapping("/update")
    public Result<Integer> updateCart(@RequestBody Cart cart){
        Integer rows=cartService.update(cart);
        if (rows!=0){
            return Result.success(rows);
        }
        return Result.error("修改数量失败");
    }

    @DeleteMapping("/remove")
    public Result<Integer> removeCart(@RequestParam Integer businessId,
                                      @RequestParam(required = false) Integer foodId,
                                      @RequestParam String userId){
        Integer rows=cartService.removeCart(userId,businessId,foodId);
        if (rows!=0){
            return Result.success(rows);
        }
        return Result.error("删除失败");

    }


}
